package com.ctms.system.controller;

import com.ctms.common.utils.PageUtils;
import com.ctms.common.utils.Query;
import com.ctms.common.utils.R;
import com.ctms.common.utils.ShiroUtils;
import com.ctms.system.domain.UserDO;
import com.ctms.system.domain.UserRoleDO;
import com.ctms.system.service.StudentService;
import com.ctms.system.service.TeacherService;
import com.ctms.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/teacher")
@Controller
public class TeacherController {

    @Autowired
    TeacherService teacherService;
    @Autowired
    StudentService studentService;
    @Autowired
    UserService userService;

    @GetMapping("")
    public String teacher(Model model){
        UserDO userDO = ShiroUtils.getUser();
        int identity = userDO.getIdentity();
        //登录姓名
        String name = userDO.getName();
        model.addAttribute("identity",identity);
        model.addAttribute("name",name);
        return "sys/teacher/teacher";
    }

    @GetMapping("/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String,Object> params){
        Query query = new Query(params);
        List<UserDO> userDOS = teacherService.selectAllTeacher(params);
        int total = teacherService.count(query);
        PageUtils pageUtils = new PageUtils(userDOS,total);
        return pageUtils;
    }

    @GetMapping("/add")
    public String add(Model model){
        return "sys/teacher/add";
    }

    @PostMapping("/save")
    @ResponseBody
    R insert(UserDO userDO){
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String time=simpleDateFormat.format(date);
        userDO.setGmtCreate(time);
        userDO.setGmtModified(time);
        userDO.setIdentity(1);
        userDO.setStatus(1);
        userDO.setPassword("123456");
        System.out.println("************************"+userDO);
        if (teacherService.insertTeacher(userDO)>0){
            int user_id = studentService.selectMaxIdByTime();
            int role_id = 2;
            UserRoleDO userRoleDO=new UserRoleDO();
            userRoleDO.setUserId(Long.parseLong(Integer.toString(user_id)));
            userRoleDO.setRoleId(Long.parseLong(Integer.toString(role_id)));
            teacherService.insertUserRole(userRoleDO);
            return R.ok();
        }else {
            return R.error();
        }
    }

    @GetMapping("/edit/{id}")
    public String edit(Model model,@PathVariable("id") Integer id){
        UserDO userDO = teacherService.selectById(id);
        if (userDO.getTeacherFrom() != null ){
            //避免出现矛盾
            if (userDO.getTeacherFrom()==0){
                userDO.setTeacherCompany("");
                teacherService.updateCompany(userDO);
            }else if (userDO.getTeacherFrom()==1){
                userDO.setTeacherCollege("");
                teacherService.updateCollege(userDO);
            }
        } else {
            userDO.setTeacherFrom(-1);
            userDO.setTeacherCompany("");
            userDO.setTeacherCollege("");
            teacherService.updateCompany(userDO);
            teacherService.updateCollege(userDO);
        }
        if (userDO.getSex() == null){
            userDO.setSex(-1);
        }
        model.addAttribute("teacher",userDO);
        return "sys/teacher/edit";
    }

    @PostMapping("/update")
    @ResponseBody
    R update(UserDO userDO){
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String time=simpleDateFormat.format(date);
        userDO.setGmtModified(time);
        if (teacherService.updateTeacher(userDO)>0){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("/remove")
    @ResponseBody
    R delete(Integer id){
        if (teacherService.delete(id)>0){
            teacherService.deleteUserRole(id);
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("/getNameById")
    @ResponseBody
    public String getNameById(Integer id){
        UserDO userDO = userService.selectUserById(id);
        String name = userDO.getName();
        return name;
    }
}
